﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;

namespace EthnicEdu.Pages
{
    public partial class ViewerQuestion : PhoneApplicationPage 
    {

        public ViewerQuestion()
        {
            InitializeComponent();

            App.myService.GetAllAnswerByQuestionIdAsync(App.ListQuestion[App.SelectQuestionIndex].QuestionId);
           App.myService.GetAllAnswerByQuestionIdCompleted += new EventHandler<Service.GetAllAnswerByQuestionIdCompletedEventArgs>(myService_GetAllAnswerByQuestionIdCompleted);
		
            ImageBrush imgBrush = new ImageBrush();

            imgBrush.ImageSource = new BitmapImage(new Uri(string.Format(@"/EthnicEdu;component/Themes/{0}.jpg",App.arrImage[Convert.ToInt32(App.ListQuestion[App.SelectQuestionIndex].ThemeId)+1].ToString()),UriKind.Relative ));
            LayoutRoot.Background = imgBrush;
            //LayoutRoot.Background = 
         

        }
        private int count;

        void myService_GetAllAnswerByQuestionIdCompleted(object sender, Service.GetAllAnswerByQuestionIdCompletedEventArgs e)
        {
            App.ListQuestion[App.SelectQuestionIndex].Answers = e.Result;
            PageTitle.Text = App.ListQuestion[App.SelectQuestionIndex].Title;
            this.DataContext =  App.ListQuestion[App.SelectQuestionIndex];
            
        }


        private void btnsubmit_Click(object sender, EventArgs e)
        {
            if (count == 0)
            {

                if (lstAnswerSingle.SelectedIndex != -1)
                {
                    App.myService.AddAccountAnswerAsync(App.AccountID, (lstAnswerSingle.SelectedItem as Service.Answer).AnswerId, DateTime.Today);
                    App.myService.AddAccountAnswerCompleted += new EventHandler<Service.AddAccountAnswerCompletedEventArgs>(myService_AddAccountAnswerCompleted);
                    count++;
                }
                else
                {
                    MessageBox.Show("Please Choose Your Answer");
                }

            }
            else if (count == 1) // next question
            {
                if (App.SelectQuestionIndex < App.ListQuestion.Count - 1)
                {
                    count = 0;
                    App.SelectQuestionIndex++;
                    App.myService.GetAllAnswerByQuestionIdAsync(App.ListQuestion[App.SelectQuestionIndex].QuestionId);
                    App.myService.GetAllAnswerByQuestionIdCompleted += new EventHandler<Service.GetAllAnswerByQuestionIdCompletedEventArgs>(myService_GetAllAnswerByQuestionIdCompleted);
                    
                    tbkComment.Text = "";
                    tbkResult.Text = "";
                    
                }
                else
                {
                    MessageBox.Show("Het cau hoi");
                }
                
            }


        }

        void myService_AddAccountAnswerCompleted(object sender, Service.AddAccountAnswerCompletedEventArgs e)
        {
            
            tbkResult.Text = "Bảng thống kê\n";
            for (int i = 0; i < App.ListQuestion[App.SelectQuestionIndex].Answers.Count; i++)
            {
                tbkResult.Text += "Đáp án " + (i + 1).ToString() + ": " + App.ListQuestion[App.SelectQuestionIndex].Answers[i].CountAnswer.ToString() + " phiếu \n";
                
            }
            if (string.IsNullOrWhiteSpace(App.ListQuestion[App.SelectQuestionIndex].Recomment))
            {
                tbkComment.Text = App.ListQuestion[App.SelectQuestionIndex].Recomment;
            }
           
        }

        private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
           // MessageBox.Show( (lstAnswerSingle.SelectedItem as Service.Answer).AnswerId.ToString());
        }

        private void lstAnswerSingle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
           
        }

       
    }
}